package davidlauzon.activerecord.connection;

import java.sql.SQLException;

import davidlauzon.activerecord.connection.ConnectionAdapter;
import davidlauzon.activerecord.recordset.RecordSet;

import davidlauzon.activerecord.AbstractTestCase;
import davidlauzon.activerecord.Constants;


public abstract class ConnectionAdapterTest extends AbstractTestCase
{
    /*********************************************************************************************
     * VARIABLES
     *********************************************************************************************/
    protected String _dropTableSQL;
    protected String _createTableSQL;
    protected String _insertSQL;
    protected String _selectSQL;
    protected String _updateSQL;
    protected String _deleteSQL;
    
    
    
    /*********************************************************************************************
     * TEST SETUP
     *********************************************************************************************/
    protected void setUp() {
        _dropTableSQL   = Constants.DROP_PEOPLE_TABLE;
        
        _createTableSQL = Constants.CREATE_PEOPLE_TABLE;
        
        _insertSQL = "INSERT INTO people (name,size,age) VALUES ('%s',%f,%d)";
        
        _selectSQL = Constants.SELECT_ALL_PEOPLE;
        
        _updateSQL = "UPDATE people SET age = 99 WHERE age > 27";
        
        _deleteSQL = "DELETE FROM people WHERE age <= 27";
    }
    
    
    
    /*********************************************************************************************
     * TEST METHODS
     *********************************************************************************************/
    public void defaultTestExecutePrep() throws SQLException {
        int    result;
        
        result = getAdapter().executeUpdate(_dropTableSQL);
        logSql(_dropTableSQL, result);
        assertNotNull("Cannot drop table.", result);
        
        result = getAdapter().executeUpdate(_createTableSQL);
        logSql(_createTableSQL, result);
        assertNotNull("Cannot create table.", result);
    }
    
    public void defaultTestExecuteInsert() throws SQLException {
        String sql;
        long   lastInsertId;
        
        sql          = String.format(_insertSQL, "David", 3.14159f, 28);
        lastInsertId = getAdapter().executeInsert(sql);
        logSql(sql, lastInsertId);
        assertNotNull("Cannot insert record.", lastInsertId);
        assertEquals("Since we just created the table, we should have id=1", 1, lastInsertId);
        
        sql          = String.format(_insertSQL, "Kali", 3.69f, 24);
        lastInsertId = getAdapter().executeInsert(sql);
        assertEquals("Testing auto-increment", 2, lastInsertId);
        
        sql          = String.format(_insertSQL, "Martin", 3.13f, 33);
        lastInsertId = getAdapter().executeInsert(sql);
        
        sql          = String.format(_insertSQL, "Myriam", 3.27f, 27);
        lastInsertId = getAdapter().executeInsert(sql);
        assertEquals("Testing auto-increment", 4, lastInsertId);
    }
    
    public void defaultTestExecuteQuery() throws SQLException {
        int size = printRecords();
        assertEquals("Should have 4 records", 4, size);
    }
    
    public void defaultTestExecuteUpdate() throws SQLException {
        int nbAffectedRows;
        int nbRecords;
        
        nbAffectedRows = getAdapter().executeUpdate(_updateSQL);
        logSql(_updateSQL, nbAffectedRows);
        printRecords();
        assertEquals("Should get 2 affected rows.", 2, nbAffectedRows);

        nbAffectedRows = getAdapter().executeUpdate(_deleteSQL);
        logSql(_deleteSQL, nbAffectedRows);
        nbRecords = printRecords();
        assertEquals("Should have deleted 2 rows.", 2, nbAffectedRows);
        assertEquals("Should have 2 rows left.", 2, nbRecords);
    }
    
    
    /*********************************************************************************************
     * INTERNAL METHODS
     *********************************************************************************************/
}
